package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.OrganizationDao;
import com.woniuxy.entity.Merchants;
import com.woniuxy.entity.Organization;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationServiceImpl implements OrganizationService {
    @Autowired(required = false)
    OrganizationDao organizationDao;

    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Organization> list = organizationDao.findAll();
        PageInfo<Organization> pageInfo = PageInfo.of(list);
        return new ResponseEntity(200,"查询总数成功",pageInfo);
    }

    @Override
    public ResponseEntity findType() {
        return new ResponseEntity(200,"查询组织类别成功",organizationDao.findType());
    }

    @Override
    public ResponseEntity findCategory() {
        return new ResponseEntity(200,"查询组织分类成功",organizationDao.findCategory());
    }

    @Override
    public ResponseEntity findAttr() {
        return new ResponseEntity(200,"查询组织属性成功",organizationDao.findAttr());
    }

    @Override
    public ResponseEntity addOne(Organization organization) {
        if(organizationDao.addOne(organization) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity updateOne(Organization organization) {
        if(organizationDao.updateOne(organization) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        if(organizationDao.deleteOne(id) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findByContext(Organization organization) {
        List<Organization> list = organizationDao.findByContext(organization);
        return new ResponseEntity(200,"查询成功",list);
    }

    @Override
    public ResponseEntity findParent(){
        List<Organization> list = organizationDao.findParent();
        return new ResponseEntity(200,"查询父类组织成功",list);
    }

    @Override
    public ResponseEntity findOrganization() {
        List<Organization> list = organizationDao.findAll();
        return new ResponseEntity(200,"查询所有组织成功",list);
    }

    @Override
    public ResponseEntity batchDelete(int[] ids) {
        if(organizationDao.batchDelete(ids) > 0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity importExcel(List<Organization> list) {
        int index = organizationDao.addBatch(list);
        if (index>0){
            return ResponseEntity.SUCCESS;
        }
        return ResponseEntity.Fail;
    }

    @Override
    public ResponseEntity findAll() {
        List<Organization> list = organizationDao.findAll();
        return new ResponseEntity<>(200,"成功",list);
    }

    @Override
    public ResponseEntity findByNo(String no) {
        String org_no = organizationDao.findByNo(no);
        if (org_no!=null){
            return new ResponseEntity(200,"成功",org_no);
        }
        return ResponseEntity.Fail;
    }
}
